// pages/regimental-commander/home/home.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeData: {},
    storeInfo: {},
    userInfo: {},
    orderNum: [],
    shareShow: false,
    orderMenus: [{ 
      id: 1,
      imgSrc: "/image/team/logistics.png",
      text: "物流配送",
      linkType: "navigateTo",
      url: "/pages/regimental-commander/logistics-delivery/logistics-delivery"
    },{
      id: 2,
      imgSrc: "/image/team/notice.png",
      text: "提醒提货",
    },{
      id: 3,
      imgSrc: "/image/team/waiting.png",
      text: "待提货",
      linkType: "navigateTo",
      total:0,
      url: "/pages/regimental-commander/order/order?orderType=waitDeliver"
    },{
      id: 4,
      imgSrc: "/image/team/after-sales.png",
      text: "售后",
      total:0,
      linkType: "navigateTo",
      url: "/pages/regimental-commander/order/order?orderType=waitRefund"
    },{
      id: 5,
      imgSrc: "/image/team/order.png",
      text: "全部订单",
      linkType: "navigateTo",
      url: "/pages/regimental-commander/order/order?orderType=index"
    }],
    service: [{
      id: 1,
      title: "送货上门",
      sub: "送货上门，提升门店服务",
      linkType: "navigateTo",
      url: "/pages/regimental-commander/delivery-tohome/delivery-tohome"
    }],
    menus: [
    //   {
    //   id: 1,
    //   imgSrc: "/image/team/invite.png",
    //   text: "邀请赚钱"
    // },
    // {
    //   id: 2,
    //   imgSrc: "/image/team/message.png",
    //   text: "消息中心"
    // },
    {
      id: 3,
      imgSrc: "/image/team/freeze.png",
      text: "冻品申请",
      linkType: "navigateTo",
      url: "/pages/regimental-commander/frozen-application/frozen-application"
    },
    // {
    //   id: 4,
    //   imgSrc: "/image/team/administor.png",
    //   text: "门店管理员"
    // },
    {
      id: 5,
      imgSrc: "/image/team/store.png",
      text: "门店歇业",
      linkType: "navigateTo",
      url: "/pages/regimental-commander/store-closure/store-closure"
    },{
      id: 9,
      imgSrc: "/image/team/store-cancellation.png",
      text: "关闭门店",
      linkType: "navigateTo",
      url: "/pages/regimental-commander/store-cancellation/store-cancellation"
    },{
      id: 6,
      imgSrc: "/image/team/customer.png",
      text: "联系客服"
    },{
      id: 7,
      imgSrc: "/image/team/feedback.png",
      text: "意见反馈",
      linkType: "navigateTo",
      url: "/pages/regimental-commander/feedback-home/feedback-home"
    },{
      id: 8,
      imgSrc: "/image/team/setting.png",
      text: "门店设置",
      linkType: "navigateTo",
      url: "/pages/regimental-commander/store-setting/store-setting"
    }]
  },
  
  /**
   * 复制
   * @param {Object} e 
   */
  copy: function(e) {
    const content = String(e.currentTarget.dataset.copyContent)
    wx.setClipboardData({
      data: content,
    })
  },

  /**
   * 链接跳转
   * @param {Object} e 
   */
  linkJump: function(e) {
    const id = e.currentTarget.dataset.id
    const datas = e.currentTarget.dataset.datas
    // 提醒提货
    if (id == 2 && datas == 'orderMenus') {
      this.setData({
        shareShow: true
      })
      return
    }
    // 数据项
    const targetData = this.data[datas].find(item => item.id == id)
    // 没有链接类型 返回
    if (!targetData) return
    // navigateTo跳转，需要url
    if (targetData.linkType == "navigateTo" && targetData.url) {
      wx.navigateTo({
        url: targetData.url,
      })
    } else {
      wx.showToast({
        title: '暂未开放...',
        icon: 'none'
      })
    }
  },

  /**
   * 链接跳转
   * @param {Object} e 
   */
  jumpLink: function (e) {
    const { url } = e.currentTarget.dataset
    wx.navigateTo({ url })
  },

  /**
   * 关闭分享弹窗
   */
  hideShare: function() {
    this.setData({
      shareShow: false
    })
  },

  /**
   * 获取门店数据
   */
  getStoreData: function() {
    const url = app.getNetAddresss('member.member.getUserInfoPackageDeliver')
    const _this = this
    wx.showLoading({ title: '门店数据获取中...' })
    app._postNetWork({
      url,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          _this.data.orderMenus[2].total= resData.data.wait_deliver
          _this.data.orderMenus[3].total= resData.data.refund_num
          _this.setData({
            storeData: resData.data,
            orderNum:_this.data.orderMenus
          })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 获取门店信息
   */
  getStoreInfo: function() {
    const urlStr = app.getNetAddresss('plugin.package-deliver.frontend.Deliver.getDeliver')
    wx.showLoading({ title: '门店信息获取中...' })
    const _this = this
    app._postNetWork({
      url: urlStr,
      success: (res) => {
        var resData = res.data;
        if (resData.result == 1) {
          this.setData({ storeInfo: resData.data.deliver })
        } else {
          wx.showToast({icon: 'none', title: resData.msg })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },
  
  /**
   * 获取用户信息
   */
  getUserData: function() {
    const urlStr = app.getNetAddresss('member.member.member-data')
    wx.showLoading({ title: '用户信息获取中...' })
    const _this = this
    app._postNetWork({
      url: urlStr,
      success: (res) => {
        var resData = res.data;
        if (resData.result == 1) {
          this.setData({ userInfo: resData.data })
        } else {
          wx.showToast({icon: 'none', title: resData.msg })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getStoreData()
    this.getStoreInfo()
    this.getUserData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(res) {
    if (res.from == 'button') {
      return {
        withShareTicket: true,
        title: '@所有人 今日商品已送达到店，请点击查看提货单',
        path: '/pages/regimental-commander/order/order?orderType=waitDeliver',
        imageUrl: '/image/team/pick-up-reminder.png',
      }
    }
  }
})